<?php 
if(KT!='KhiThe') exit;
class products_controller extends products_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
		parent::addObject('khithe_paging');
		parent::addObject('khithe_images');
	}
	
	public function index_action()
	{
		$limit=24;
		$resultHtml = NULL;		
		$arrhtmlstr = explode('.',$this->getUrlEnd());
		if(!empty($arrhtmlstr[1]))
		{
			/* IF HAS TYPE .HTML */
			$seokey = $this->getUrlNextEnd(1);
			$struri = $this->getUrl(2);
			
		}
		else{
			/* IF NO TYPE .HTML */
			$seokey = $this->getUrlEnd();
			$struri = $this->getUrl(2);
		} 

		$parentID = parent::getIDSk($seokey);
		$arrMenu = parent::getMenu($parentID);
		$this->lang["title-category"]=$arrMenu['Category']!=""?$arrMenu['Category']:$this->lang['TitleMenu'];
		$totalRows = mysql_num_rows(parent::getSqlProducts($parentID,'','')); 
		$strPaging = $this->display_paging($totalRows, intval($this->getUrlEnd()), intval($limit), url_base.$struri.'', $this->lang, '/','.html');
		$this->lang['paging'] = $strPaging; 
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultProduct = parent::getSqlProducts($parentID, intval($this->getUrlEnd()), intval($limit) );
		while( $rowProduct = mysql_fetch_assoc($resultProduct) )
		{ 
			if($rowProduct['PriceSpecial']>0)
			{
				$pp = number_format($rowProduct['PriceSpecial']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"];
				$ppv = $rowProduct['PriceSpecial']/$this->lang["rate"];
				$ps = $rowProduct['Price']!=0?number_format($rowProduct['Price']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"]:$this->lang["Call"];
			}else{
				$ps = '';
				$pp = $rowProduct['Price']!=0?number_format($rowProduct['Price']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"]:$this->lang["Call"];
				$ppv = $rowProduct['Price']!=0?$rowProduct['Price']/$this->lang["rate"]:0;
			}
			$fileImg = path_base.'/uploads/products/'.$rowProduct['Picture'];			
			$fileThumbImg = path_base.'/uploads/products/cache/'.$rowProduct['Picture'];
			if(!is_file($fileThumbImg) && is_file($fileImg))
			{
				$this->setIMG($fileImg);
				$this->resizeImage(200, 180, 'auto');
				$this->saveImage($fileThumbImg);
			}
			$ThumbImg = url_base.'uploads/products/cache/'.$rowProduct['Picture'];
			if(!is_file($fileThumbImg)){
				$ThumbImg =url_base.'uploads/products/cache/logo.png';
			}
			$arrProduct = array(
				"product-id"=>$rowProduct['ID'],
				"url-product"=>url_base . ($this->seo_list_parent(categorys,NULL,$rowProduct['CategoryID'])).$rowProduct['SeoKey'].'.html',
				"Title-full"=>$rowProduct['Title'],
				"Title-short"=>$rowProduct['Title'],
				"product-img"=>url_base.'uploads/products/'.$rowProduct['Picture'],
				"product-thumb-img"=>$ThumbImg,
				"price-del"=>$ps,
				"price-primary"=>$pp,
				"price-primary-val" =>$ppv
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['product'],$arrProduct);

		} mysql_free_result($resultProduct);
		
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlproduct'=>$resultHtml));
		$this->lang['dis'] ='';
		$this->lang['notpro'] = '';
		if(empty($totalRows)){
			$this->lang['dis'] = 'disabled="disabled"';
			$this->lang['notpro'] = '<h5 style="color:#F00">'.$this->lang['errnot'].'</h5>';
		}
		
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		
		if(!empty($arrMenu['Category']))
		{
			$arrAct = array(
				'html'=>$strHtmlContent,
				'title_paper'=>$arrMenu['TitlePage'],
				'keywords'=>$arrMenu['KeyWords'],
				'description'=>$arrMenu['Description'],
				'SeoH1'=>$arrMenu['SeoH1'],
				'SeoH2'=>$arrMenu['SeoH2'],
				'SeoP'=>$arrMenu['SeoP']
			);

		}else{
			$arrAct = $strHtmlContent;
		}
			
		return $arrAct;
		
		
	}
	
	public function view_action()
	{
		$strHtmlContent = $this->khithe_display('view');
		$img_thumb =null;
		$this->lang['dis_price'] ='';
		$arrLink = explode('.',$this->getUrlEnd());
		$rowProduct = parent::selectProduct(trim($arrLink[0]),intval($this->valUrl(2)));
		if($rowProduct['PriceSpecial']>0){	
			$ps = number_format($rowProduct['PriceSpecial']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"];
			$pp = $rowProduct['Price']!=0?number_format($rowProduct['Price']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"]:$this->lang["Call"];
			$ppv = $rowProduct['Price']!=0?$rowProduct['Price']/$this->lang["rate"]:0;
		
		}else{
			$ps = '';
			$this->lang['dis_price'] ='none';
			$pp = $rowProduct['Price']!=0?number_format($rowProduct['Price']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"]:$this->lang["Call"];
			$ppv = $rowProduct['Price']!=0?$rowProduct['Price']/$this->lang["rate"]:0;
		}
		$RowManufac = parent::getManufacturer($rowProduct['ManufacturerID']);
		
				
		$fileImg = path_base.'/uploads/products/'.$rowProduct["Picture"];					
		$fileThumbImg = path_base.'/uploads/products/cache/'.$rowProduct["Picture"];
		if(!is_file($fileThumbImg) && is_file($fileImg))
		{
			$this->setIMG($fileImg);
			$this->resizeImage(200, 180, 'auto');
			$this->saveImage($fileThumbImg);
		}
		
		$fileImg1 = path_base.'/uploads/products/'.$rowProduct["Picture_1"];					
		$fileThumbImg1 = path_base.'/uploads/products/cache/'.$rowProduct["Picture_1"];
		if(!is_file($fileThumbImg1) && is_file($fileImg1))
		{
			$this->setIMG($fileImg1);
			$this->resizeImage(200, 180, 'auto');
			$this->saveImage($fileThumbImg1);
		}
		
		$fileImg2 = path_base.'/uploads/products/'.$rowProduct["Picture_2"];					
		$fileThumbImg2 = path_base.'/uploads/products/cache/'.$rowProduct["Picture_2"];
		if(!is_file($fileThumbImg2) && is_file($fileImg2))
		{
			$this->setIMG($fileImg2);
			$this->resizeImage(200, 180, 'auto');
			$this->saveImage($fileThumbImg2);
		}
		
		$fileImg3 = path_base.'/uploads/products/'.$rowProduct["Picture_3"];					
		$fileThumbImg3 = path_base.'/uploads/products/cache/'.$rowProduct["Picture_3"];
		if(!is_file($fileThumbImg3) && is_file($fileImg3))
		{
			$this->setIMG($fileImg3);
			$this->resizeImage(200, 180, 'auto');
			$this->saveImage($fileThumbImg3);
		}
		
		$fileImg4 = path_base.'/uploads/products/'.$rowProduct["Picture_4"];					
		$fileThumbImg4 = path_base.'/uploads/products/cache/'.$rowProduct["Picture_4"];
		if(!is_file($fileThumbImg4) && is_file($fileImg4))
		{
			$this->setIMG($fileImg4);
			$this->resizeImage(200, 180, 'auto');
			$this->saveImage($fileThumbImg4);
		}
		
		$ThumbImg = url_base.'uploads/products/cache/'.$rowProduct['Picture'];
		$BigImg = url_base.'uploads/products/'.$rowProduct['Picture'];
		if(!is_file($fileThumbImg)){
			$BigImg = url_base.'uploads/products/cache/logo.png';
			$ThumbImg =url_base.'uploads/products/cache/logo.png';
		}
		
		$stredit ='';
		if(isset($_SESSION['Mod'])&&$_SESSION['Mod']=='Admin'){
			$stredit =' | <a style="text-decoration:none;" 
				href="'.url_base.'admin/products/edit/'.$rowProduct['ID'].'&iframe=true" 
				onclick="return hs.htmlExpand(this, {
					objectType: \'iframe\', width: 800, height: 550,
					creditsPosition: \'bottom left\', 
					headingEval: \'this.a.title\',
					wrapperClassName: \'titlebar\' } )" 
				title="Edit-Products"><small>edit</small></a>';
		}
		
		if(!(empty($rowProduct['Picture_1']) && empty($rowProduct['Picture_2']) && empty($rowProduct['Picture_3'])&&empty($rowProduct['Picture_4']))){
			$img_thumb =  $rowProduct['Picture']==''?'':'<li><img id="img_0" class="img_small" onmouseover="change_img(\'img_0\',\''.url_base.'uploads/products/'.$rowProduct['Picture'].'\',\''.url_base.'uploads/products/cache/'.$rowProduct['Picture'].'\')" width="43" height="35" alt="'.$rowProduct['Title'].'" src="'.url_base.'uploads/products/cache/'.$rowProduct['Picture'].'" />';
			$img_thumb .= $rowProduct['Picture_1']==''?'':'<li><img id="img_1" class="img_small" onmouseover="change_img(\'img_1\',\''.url_base.'uploads/products/'.$rowProduct['Picture_1'].'\',\''.url_base.'uploads/products/cache/'.$rowProduct['Picture_1'].'\')" width="43" height="35" alt="'.$rowProduct['Title'].'" src="'.url_base.'uploads/products/cache/'.$rowProduct['Picture_1'].'" />';
			$img_thumb .= $rowProduct['Picture_2']==''?'':'<li><img id="img_2" class="img_small" onmouseover="change_img(\'img_2\',\''.url_base.'uploads/products/'.$rowProduct['Picture_2'].'\',\''.url_base.'uploads/products/cache/'.$rowProduct['Picture_2'].'\')" width="43" height="35" alt="'.$rowProduct['Title'].'" src="'.url_base.'uploads/products/cache/'.$rowProduct['Picture_2'].'" />';
			$img_thumb .= $rowProduct['Picture_3']==''?'':'<li><img id="img_3" class="img_small" onmouseover="change_img(\'img_3\',\''.url_base.'uploads/products/'.$rowProduct['Picture_3'].'\',\''.url_base.'uploads/products/cache/'.$rowProduct['Picture_3'].'\')" width="43" height="35" alt="'.$rowProduct['Title'].'" src="'.url_base.'uploads/products/cache/'.$rowProduct['Picture_3'].'" />';
			$img_thumb .= $rowProduct['Picture_4']==''?'':'<li><img id="img_4" class="img_small" onmouseover="change_img(\'img_4\',\''.url_base.'uploads/products/'.$rowProduct['Picture_4'].'\',\''.url_base.'uploads/products/cache/'.$rowProduct['Picture_4'].'\')" width="43" height="35" alt="'.$rowProduct['Title'].'" src="'.url_base.'uploads/products/cache/'.$rowProduct['Picture_4'].'" />';
		}
		
		$arrProduct = array(
			"product-id"=>$rowProduct['ID'],
			"edit_pro"=>$stredit,
			"Title-full"=>$rowProduct['Title'],
			"Title-short"=>$this->cutString($rowProduct['Title'],20),
			"product-img"=>$BigImg,
			"img_thumb"=>$img_thumb,
			"product-thumb-img"=>$ThumbImg,
			"price-del"=>$ps==''?0:$ps,
			"price-primary"=>$ps==''?$pp:'<del>'.$pp.'</del>',
			"price-primary-val" =>$ppv,
			"contentsSpecifications"=>$rowProduct['Details'],
			"Manufacturer"=>$RowManufac['Manufacturer'],
			"Warranty"=>(double)$rowProduct['Warranty'].' '.$this->lang['moth'],
			"Promotion"=>$rowProduct['Promotion']==''?$this->lang['notPromotion']:$rowProduct['Promotion'],
			"Products-same-type"=>$this->productSameType($rowProduct['CategoryID']),
			"url-product"=>url_base.(parent::seo_list_parent(categorys,NULL,$rowProduct['CategoryID'])).$rowProduct['SeoKey'].'.html',
		);
		$strHtmlContent = $this->khithe_assign($strHtmlContent,$arrProduct);
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		
		$arrAction = array(
			'html'=>$strHtmlContent,
			'title_paper'=>$rowProduct['TitlePage'],
			'keywords'=>trim($rowProduct['KeyWords']),
			'description'=>$this->getDescription($rowProduct['Description']),
			'SeoH1'=>$rowProduct['SeoH1'],
			'SeoH2'=>$rowProduct['SeoH2'],
			'SeoP'=>$rowProduct['SeoP'],	
		);
		return $arrAction;
		
	}
	
	private function productSameType($parentID)
	{
		$limit=16;
				
		$arrhtmlstr = explode('.',$this->getUrlEnd());
		if(!empty($arrhtmlstr[1]))
		{
			/* IF HAS TYPE .HTML */
			$seokey = $this->getUrlNextEnd(1);
			$struri = $this->getUrl(2);
			
		}
		else{
			/* IF NO TYPE .HTML */
			$seokey = $this->getUrlEnd();
			$struri = $this->getUrl(2);
		} 
		
		$parentID = parent::getIDSk($seokey);
		$arrMenu = parent::getMenu($parentID);
		$this->lang["title-category"]=$arrMenu['Category']!=""?$arrMenu['Category']:$this->lang['TitleMenu'];
		$totalRows = mysql_num_rows(parent::getSqlProducts($parentID,'','')); 
		$strPaging = $this->display_paging($totalRows, intval($this->getUrlEnd()), intval($limit), url_base.$struri.'', $this->lang, '/','.html');
		$this->lang['paging'] = $strPaging; 
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultProduct = parent::getSqlProducts(intval($parentID), intval($this->getUrlEnd()), intval($limit) );
		$resultHtml = NULL;
		while( $rowProduct = mysql_fetch_assoc($resultProduct) )
		{ 
			if($rowProduct['PriceSpecial']>0)
			{
				$pp = number_format($rowProduct['PriceSpecial']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"];
				$ppv = $rowProduct['PriceSpecial']/$this->lang["rate"];
				$ps = $rowProduct['Price']!=0?number_format($rowProduct['Price']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"]:$this->lang["Call"];
			}
			else{
				$ps = '';
				$pp = $rowProduct['Price']!=0?number_format($rowProduct['Price']/$this->lang["rate"],0,'.',',').' '.$this->lang["rates"]:$this->lang["Call"];
				$ppv = $rowProduct['Price']!=0?$rowProduct['Price']/$this->lang["rate"]:0;
			}
			$fileImg = path_base.'/uploads/products/'.$rowProduct['Picture'];			
			$fileThumbImg = path_base.'/uploads/products/cache/'.$rowProduct['Picture'];
			if(!is_file($fileThumbImg) && is_file($fileImg))
			{
				$this->setIMG($fileImg);
				$this->resizeImage(200, 180, 'auto');
				$this->saveImage($fileThumbImg);
			}
			$ThumbImg = url_base.'uploads/products/cache/'.$rowProduct['Picture'];
			$BigImg = url_base.'uploads/products/'.$rowProduct['Picture'];
			if(!is_file($fileThumbImg)){
				$BigImg = url_base.'uploads/products/cache/logo.png';
				$ThumbImg =url_base.'uploads/products/cache/logo.png';
			}
			$arrProduct = array(
				"product-id"=>$rowProduct['ID'],
				"url-product"=>url_base.(parent::seo_list_parent(categorys,NULL,$rowProduct['CategoryID'])).$rowProduct['SeoKey'].'.html',
				"Title-full"=>$rowProduct['Title'],
				"Title-short"=>$rowProduct['Title'],
				"product-img"=>$BigImg,
				"product-thumb-img"=>$ThumbImg,
				"price-del"=>$ps,
				"price-primary"=>$pp,
				"price-primary-val" =>$ppv
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['product'],$arrProduct);

		} mysql_free_result($resultProduct);
		
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlproduct'=>$resultHtml));
		$this->lang['dis'] ='';
		$this->lang['notpro'] = '';
		if(empty($totalRows))
		{
			$this->lang['dis'] = 'disabled="disabled"';
			$this->lang['notpro'] = '<h3 style="color:#F00">'.$this->lang['errnot'].'</h3>';
		}
		
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
				
		return $strHtmlContent;
	}
	
	public function index_ajax_action()
	{	
	
	}


}
?>